package com.njlime.sys.repository;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.entities.UnitWxConfig;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dean on 2017-7-27.
 */
@Repository
public class UnitWxConfigDaoImpl extends BaseDaoImpl<UnitWxConfig> {
    /**
     * 根据单位获取微信设置
     * @return
     */
    public List<UnitWxConfig> getWxConfigList(){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_unit_wx_config  ");
        return findList(sb.toString(),arrList);
    }
    /**
     * 根据单位获取微信设置
     * @param unit_id
     * @return
     */
    public UnitWxConfig getWxConfig(Integer unit_id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_unit_wx_config where unit_id = ? ");
        arrList.add(unit_id);
        return findOne(sb.toString(),arrList);
    }

    /**
     * 根据单位获取微信设置
     * @param appid
     * @return
     */
    public UnitWxConfig getWxConfigByAppid(String appid){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_unit_wx_config where app_id = ? ");
        arrList.add(appid);
        return findOne(sb.toString(),arrList);
    }

    /**
     * 编辑单位微信个公共号
     * @param unit
     * @return
     */
    public int editUnitConfig(UnitWxConfig unit){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_unit_wx_config set update_time = sysdate() ");

        if(!StringUtil.isEmpty(unit.getApp_id())){
            sb.append(",app_id = ? ");
            arrList.add(unit.getApp_id());
        }
        if(!StringUtil.isEmpty(unit.getApp_secret())){
            sb.append(",app_secret = ? ");
            arrList.add(unit.getApp_secret());
        }
        if(!StringUtil.isEmpty(unit.getMch_id())){
            sb.append(",mch_id = ? ");
            arrList.add(unit.getMch_id());
        }
        if(!StringUtil.isEmpty(unit.getMch_key())){
            sb.append(",mch_key = ? ");
            arrList.add(unit.getMch_key());
        }
        if(!StringUtil.isEmpty(unit.getCert())){
            sb.append(",cert = ? ");
            arrList.add(unit.getCert());
        }

        if(!StringUtil.isEmpty(unit.getTt_key())){
            sb.append(",tt_key = ? ");
            arrList.add(unit.getTt_key());
        }

        if(!StringUtil.isEmpty(unit.getWx_id())){
            sb.append(",wx_id = ? ");
            arrList.add(unit.getWx_id());
        }
        if(!StringUtil.isEmpty(unit.getTt_secret())){
            sb.append(",tt_secret = ? ");
            arrList.add(unit.getTt_secret());
        }
        sb.append(" where unit_id = ?");
        arrList.add(unit.getUnit_id());
        return updateJdbc(sb.toString(),arrList);
    }


}
